﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MySql;
using MySql.Data.MySqlClient;
using System;
using System.Data;
using UnityEngine.UI;

public class MySqlSeverMain : MonoBehaviour
{
    public MysqlInfo mysqlInfo;
    public Scrollbar scrollbar;
    public float va
    {
        set
        {
            if (value > 0)
            {
                a = value.ToString();
                Debug.Log(a);
            }
            else
            {
                Debug.Log("传入参数错误");
            }
        }
        get
        {
            return va;
        }
    }
    private string a;
    public DataTable dataTable;//设备点位表
    public static MySqlSeverMain instance;
    public bool isBuile;//是否打包
    private void Awake()
    {
        instance = this;
        if (isBuile)
            mysqlInfo.ipAddress = "192.168.4.123";

        MysqlHelper.ConnectMySql(mysqlInfo);
        dataTable = MysqlHelper.SelectNotNull("devicepointsdata",
                "AlarmStr");
        Debug.Log("非空查询" + dataTable.Rows.Count);
    }
    void Start()
    {

        //List<MysqlItemData> mysqlItemDatas = new List<MysqlItemData>();
        //mysqlItemDatas.Add(new MysqlItemData 
        //{
        //    columu = "name",
        //    value = "堆垛机",
        //});
        //DataTable dataTable = MysqlHelper.SelectSqlView("datatestsqlview", mysqlItemDatas);
        //Debug.Log(dataTable.Rows.Count);

        //va = 20;
        //scrollbar.onValueChanged.AddListener((f) => 
        //{
        //    va = f;
        //});
    }

    public void Connect()
    {
        MySqlConnection myCon = new MySqlConnection();
        try
        {
            if (myCon.State == ConnectionState.Closed)
            {
                myCon.Open();
            }
            //string sql = "select * from  a";
            string sql = "select value from test where name = 入库统计 and field =inouttotal";
            MySqlCommand cmd = new MySqlCommand(sql, myCon);
            // DataTable table =new DataTable();
            MySqlDataAdapter adapter = new MySqlDataAdapter(sql, myCon);
            DataTable table = new DataTable();
            //4.读取结果
            //MySqlDataReader read = cmd.ExecuteReader();
            //使用
            adapter.Fill(table);
            Debug.Log(table);//table.Rows[int]["string"]
            //read.Close();
            myCon.Close();
            Debug.Log("数据库已关闭");
        }
        catch (Exception e)
        {
            Debug.Log("打开数据库错误！" + e);

        }

    }

    /*
    //连接数据库
    public void Connect()
    {
        Debug.Log("数据库脚本开头");
        MySqlConnection conn = new MySqlConnection(connString);
        try
        {
            //打开数据库
            conn.Open();
            Debug.Log("数据库打开成功");
            //mysql命令
            string cmdstr = string.Format("select * from {0};", "a");
            MySqlCommand cmd = new MySqlCommand(cmdstr, conn);//select filling_capacity from filling_record where id=1000

            //执行查询语句
            MySqlDataReader reader = cmd.ExecuteReader();
           //判断列表是否有数据
           if (reader.HasRows)
           {
                reader.Read();
                
            //    //用账号密码示例
            //    加注量 = reader.GetString("filling_capacity");
            //    //BtObj.instance.加注txt.text = 加注量;
                Debug.Log(cmd);
            }
            reader.Close();
            conn.Close();
            Debug.Log("数据库已关闭");
        }
       catch (Exception e)
       {
           Debug.Log("打开数据库错误！" + e);
      
       }
      //mysql命令行

    }
    //关闭数据库
    public static void CloseConnection(MySqlConnection conn)
    {
        if (conn != null) conn.Close();

        else Console.WriteLine("MySqlConnection不能为空");
    }
    */

    void DataTransfer()
    {

    }

}
